#include "gd32f4xx.h"
#include "bsp_timer.h"
#include <stdio.h>


//static void gpio_config(void);
static void timer2_config(void);
/* configure the TIMER2 interrupt */
static void timer2_nvic_config(void);
/*!
    \brief      configure the GPIO ports
    \param[in]  none
    \param[out] none
    \retval     none
*/
//static void gpio_config(void)
//{
//    rcu_periph_clock_enable(RCU_GPIOB);

//    /*configure PB3(TIMER2 CH1) as alternate function*/
//    gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_3);
//    gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_3);

//    gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_3);
//}
/**
    \brief      configure the TIMER2 interrupt
    \param[in]  none
    \param[out] none
    \retval     none
*/
static void timer2_nvic_config(void)
{
    nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3);
    nvic_irq_enable(TIMER2_IRQn, 1, 1);
}

uint8_t g_timer2_flag = 0;
/*!
    \brief      configure the timer peripheral
    \param[in]  none
    \param[out] none
    \retval     none
*/
static void timer2_config(void)
{
    /* ---------------------------------------------------------------------------
    TIMER2 configuration: output compare toggle mode:
    TIMER1CLK = systemcoreclock / 10000=20K,
    CH1 update rate = TIMER2 counter clock / CH1VAL = 20000/3000 = 6 Hz
    ----------------------------------------------------------------------------*/
//    timer_oc_parameter_struct timer_ocintpara;
    timer_parameter_struct timer_initpara;

    rcu_periph_clock_enable(RCU_TIMER2);
    rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4);

    timer_deinit(TIMER2);

    /* TIMER2 configuration */
    timer_initpara.prescaler         = 9999;
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.period            = 2999;
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    timer_init(TIMER2,&timer_initpara);

     /* CH1 configuration in OC TOGGLE mode */
//    timer_ocintpara.ocpolarity  = TIMER_OC_POLARITY_HIGH;
//    timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
//    timer_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
//    timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
//    timer_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
//    timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
//    timer_channel_output_config(TIMER2,TIMER_CH_1,&timer_ocintpara);

//    timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_1,3999);
//    timer_channel_output_mode_config(TIMER2,TIMER_CH_1,TIMER_OC_MODE_TOGGLE);
//    timer_channel_output_shadow_config(TIMER2,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);

    /* auto-reload preload enable */
    timer_auto_reload_shadow_enable(TIMER2);
    
    timer2_nvic_config();
    
    timer_interrupt_flag_clear(TIMER2, TIMER_INT_FLAG_UP);
    timer_interrupt_enable(TIMER2, TIMER_INT_UP);
    timer_enable(TIMER2);
    
    timer_enable(TIMER2);
}

/*!
    \brief      bsp_timer_init function
    \param[in]  none
    \param[out] none
    \retval     none
*/
void bsp_timer_init(void)
{
    timer2_config();
}
